import 'dart:io';

import 'package:bot_toast/bot_toast.dart';
import 'package:get/get.dart';
import 'package:image_gallery_saver/image_gallery_saver.dart';
import 'package:image_picker/image_picker.dart';
import 'package:toolbox/common/common.dart';
import 'package:toolbox/page/video_edit/component/trimmerView.dart';
import 'package:toolbox/page/video_play/videoPlayView.dart';
import 'package:video_compress/video_compress.dart';

import 'state.dart';

class VideoEditLogic extends GetxController {
  final VideoEditState state = VideoEditState();

  Future<File> videoCompressAndGetFile(File file) async {
    MediaInfo? mediaInfo = await VideoCompress.compressVideo(
      file.path,
      quality: state.videoQuality,
      deleteOrigin: false,
    );
    return mediaInfo?.file ?? File('');
  }

  void onTapCompressVideo() async {
    if (state.originalFile == null) {
      BotToast.showText(text: '请先选择视频');
      return;
    }
    if (!state.originalFile!.existsSync()) {
      BotToast.showText(text: '文件不存在');
      return;
    }
    BotToast.showLoading();
    try {
      File originalFile = state.originalFile!;
      File file = await videoCompressAndGetFile(originalFile);

      print('压缩前大小${originalFile.lengthSync()}');
      print('压缩后大小${file.lengthSync()}');
      state.originalSize = originalFile.lengthSync().toDouble();
      state.compressedSize = file.lengthSync().toDouble();
      state.compressedFile = file;
      state.compressedFileThumbnail.value = await VideoCompress.getFileThumbnail(
        state.compressedFile!.path,
      );
    } catch (e) {
      print(e);
    }
    BotToast.closeAllLoading();
  }

  void onTapToTrimmer() {
    if (state.originalFile == null) {
      BotToast.showText(text: '请先选择视频');
      return;
    }
    if (!state.originalFile!.existsSync()) {
      BotToast.showText(text: '文件不存在');
      return;
    }
    Get.to(TrimmerView(file: state.originalFile!));
  }

  void onSelectedVideoQuality(VideoQuality? value) {
    if (value != null) {
      state.videoQuality = value;
    }
  }

  Future<void> onSelectedVideo() async {
    if (state.originalFile != null) {
      onTapPlayVideo(state.originalFile!);
      return;
    }

    final XFile? xFile = await state.picker.pickVideo(source: ImageSource.gallery);
    if (xFile == null) {
      return;
    } else {
      state.originalFile = File(xFile.path);
      state.originalSize = state.originalFile?.lengthSync().toDouble() ?? 0.0;
      state.compressedSize = 0.0;
      state.originalFileThumbnail.value = await VideoCompress.getFileThumbnail(
        state.originalFile!.path,
      );
      print(state.originalFileThumbnail.value!.path);
    }
  }

  Future<void> onSaveVideo() async {
    if (state.compressedFile != null) {
      if (!state.originalFile!.existsSync()) {
        BotToast.showText(text: '文件不存在');
        return;
      }
      BotToast.showLoading();
      dynamic result;
      try {
        result = await ImageGallerySaver.saveFile(state.compressedFile!.path);
        print('保存：$result');
      } catch (e) {
        print(e);
      }
      BotToast.closeAllLoading();
      BotToast.showText(text: result['isSuccess'] ? Strings.saveSuccess : Strings.saveFailed);
    } else {
      BotToast.showText(text: '文件为空');
    }
  }

  void onDeleteSelected() {
    state.originalFileThumbnail.value = null;
    state.originalFile = null;
  }

  void onTapPlayVideo(File file) {
    Get.to(VideoPlayPage(file: file));
  }
}
